#ifdef a
#include"Stack.h"

//栈初始化
void StackInit(Stack* ps, int capacity) {
	assert(ps);
	ps->capacity = capacity < 0 ? 3 : capacity;
	ps->array = (TYPESTACK*)malloc(sizeof(TYPESTACK) * ps->capacity);
	if (!ps->array) {
		assert(0);
		return;
	}
	ps->size = 0;
	return;
}

//扩容
void StackCapacity(Stack* ps) {
	assert(ps);
	if (ps->capacity == ps->size) {
		TYPESTACK* temp = (TYPESTACK*)realloc(ps->array ,sizeof(TYPESTACK) * 2 * ps->capacity);
		if (!temp) {
			assert(temp);
			return;
		}
		ps->array = temp;
		ps->capacity *= 2;
	}
	return;
}
//入栈
void StackPush(Stack* ps, TYPESTACK data) {
	StackCapacity(ps);//扩容
	ps->array[ps->size++] = data;
	return;
}

//查空
int StackIsEmpty(Stack* ps) {
	assert(ps);
	return ps->size == 0;
}

//出栈
void StackOut(Stack* ps) {
	//查空
	if (StackIsEmpty(ps))
		return;
	ps->size--;
	return;
}

//查看栈顶元素
int StackTop(Stack* ps) {
	assert(ps);//栈不为空
	return ps->array[ps->size - 1];
}

//查看栈容量
int StackMany(Stack* ps) {
	return ps->capacity;
}

//查看栈的元素个数
TYPESTACK StackSize(Stack* ps) {
	return ps->size;
}

//销毁
void StackDestory(Stack* ps) {
	if (ps->array) {
		free(ps->array);
		ps->array = NULL;
		ps->capacity = 0;
		ps->size = 0;
	}
	return;
}

//栈的打印
void StackPrint(Stack* ps) {
	if (StackIsEmpty(ps))
		printf("空\n");
	else {
		for (int i = 0; i < ps->size; i++) {
			printf("%d", ps->array[i]);
			if (i != ps->size - 1)
				printf("-->");
		}
		printf("\n");
	}
	return;
}
//----------------------------------------------------------------
//测试
void TestStack() {
	Stack s;
	StackInit(&s, 4);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	StackPrint(&s);
	printf("top = %d\n", StackTop(&s));
	printf("capacity = %d\n", StackMany(&s));
	printf("size = %d\n", StackSize(&s));
	
	printf("\n");

	StackPush(&s, 5);
	StackPrint(&s);
	printf("top = %d\n", StackTop(&s));
	printf("capacity = %d\n", StackMany(&s));
	printf("size = %d\n", StackSize(&s));

	printf("\n\n");

	StackOut(&s);
	StackOut(&s);
	StackPrint(&s);
	printf("top = %d\n", StackTop(&s));
	printf("capacity = %d\n", StackMany(&s));
	printf("size = %d\n", StackSize(&s));


	return;
}
#endif